package com.jack.payment.util.common;


import com.alibaba.fastjson.JSONObject;
import com.jack.payment.entity.business.SendEmailBo;
import com.jack.payment.entity.generate.config.ConfigEmailModel;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

@Component
public class EmailMessageUtil {
    private Logger log =  LoggerFactory.getLogger(EmailMessageUtil.class);

    // 发件配置
    private static JavaMailSender javaMailSender;
    // 系统默认
    private static String systemSendAccount;
    private static String personal;

    /**
     * 配置发件邮箱基础配置信息
     * @param configEmailModel
     * @param sender
     */
    public synchronized void initConfigEmail(ConfigEmailModel configEmailModel, JavaMailSender sender) {
        log.info("加载初始化邮箱配置, 配置数据如下==>>>{}", JSONObject.toJSON(configEmailModel));
        systemSendAccount = configEmailModel.getAccount();
        javaMailSender = sender;
        personal = configEmailModel.getPersonal();
    }

    /**
     * 初始化邮件数据
     * @param configEmailModel  邮箱数据
     */
    public JavaMailSender configMailInfo(ConfigEmailModel configEmailModel){
        log.info(">>>>>>>>>>>>配置发件邮箱");
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setDefaultEncoding("UTF-8");
        mailSender.setHost(configEmailModel.getHost());
        mailSender.setPort(configEmailModel.getPort());
        mailSender.setUsername(configEmailModel.getAccount());
        mailSender.setPassword(configEmailModel.getPassword());
        mailSender.setProtocol(configEmailModel.getProtocol());
        Properties properties = mailSender.getJavaMailProperties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.timeout", "10000");
        properties.put("mail.smtp.starttls.enable", "true");
        return mailSender;
    }


    @Async
    public void sendEmail(SendEmailBo requestEmailModel) {
        log.info("正在进行异步发送邮件, 接受邮件的账号为==>>>{}, 本次发件主题为==>>>{}",
                requestEmailModel.getAccount(),
                requestEmailModel.getSubject());
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            InternetAddress from = new InternetAddress(systemSendAccount, personal);
            message.setFrom(from);
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(String.valueOf(requestEmailModel.getAccount()));
            helper.setSubject(requestEmailModel.getSubject());
            helper.setText(requestEmailModel.getText(), true);
            javaMailSender.send(message);
        } catch (MailException | MessagingException | UnsupportedEncodingException e) {
            log.error("发送邮件失败!!! 请检查网络是否存在波动或邮件地址不存在");
            e.printStackTrace();
        }
    }

}
